package com.cdks.wuxiu.api.service.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.mapper.UserAlbumInfoMapper;
import com.cdks.wuxiu.entity.mysql.UserAlbumInfo;

import java.util.List;

import org.springframework.stereotype.Service;

/**
 * 用户相册 服务实现类
 */
@Service
public class UserAlbumInfoServiceImpl extends ServiceImpl<UserAlbumInfoMapper, UserAlbumInfo> implements UserAlbumInfoService {
    @Override
    public int deleteByUser(long userId) {
        baseMapper.deleteByUser(userId);
        
        return 0;
    }

    @Override
    public int findByName(long userId, String imageUri) {
        QueryWrapper<UserAlbumInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserAlbumInfo::getImageUri, imageUri)
                        .eq(UserAlbumInfo::getUserId, userId);
        UserAlbumInfo ret  = baseMapper.selectOne(wrapper);
        if (ret == null)
        {
            return 0;
        }
        
        return 1;
    }

    @Override
    public int deleteByName(long userId, String imageUri) {
        QueryWrapper<UserAlbumInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserAlbumInfo::getImageUri, imageUri)
                        .eq(UserAlbumInfo::getUserId, userId);
        baseMapper.delete(wrapper);
        
        return 0;
    }

    @Override
    public int refusePortrait(long userId) {
        QueryWrapper<UserAlbumInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserAlbumInfo::getType, Constant.AUDIT_TYPE_PORTRAIT)
                        .eq(UserAlbumInfo::getStatus, Constant.AUDIT_STATUS_WAIT)
                        .eq(UserAlbumInfo::getUserId, userId);
        baseMapper.delete(wrapper);

        return 0;
    }

    @Override
    public List<UserAlbumInfo> getPhotosByUser(long userId, Integer page) {
        QueryWrapper<UserAlbumInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserAlbumInfo::getUserId, userId)
                        .eq(UserAlbumInfo::getType, Constant.AUDIT_TYPE_PHOTO)
                        .eq(UserAlbumInfo::getStatus, Constant.AUDIT_STATUS_PASS)
                        .select(UserAlbumInfo::getImageUri)
                        .orderByDesc(UserAlbumInfo::getId);

        return baseMapper.selectList(wrapper.last("LIMIT " + page * Constant.LIST_PAGE_SIZE + "," + Constant.LIST_PAGE_SIZE));
    }
}
